package com.baihan.angus.app.game.busshow.pop_dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.basic.utils.ComUtil;
import com.baihan.angus.app.game.basic.utils.Tooast;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.adps.NoteAdp;
import com.baihan.angus.app.game.common.bean.response.NoteResponse;

import java.util.List;

public class NoteDialog extends Dialog {

    private Callback mCallback;
    private Context mContext;
    private WindowManager.LayoutParams mLayoutParams;
    private TextView tvTitle, tvCreateNote;
    private EditText etNote;
    private RecyclerView rvNotes;
    private ImageView ivClose, ivNoData;

    private NoteAdp mNoteAdp;
    private List<NoteResponse.Data> notes;

    public NoteDialog(@NonNull Context context, Callback mCallback) {
        super(context, R.style.FullDialog);
        this.mContext = context;
        this.mCallback = mCallback;
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        mLayoutParams.height = ComUtil.Dp2Px(context, 460);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_note, (ViewGroup) null);
        setContentView(dialogView);
        tvTitle = dialogView.findViewById(R.id.tvTitle);
        ivClose = dialogView.findViewById(R.id.ivClose);
        etNote = dialogView.findViewById(R.id.etNote);
        rvNotes = dialogView.findViewById(R.id.rvNotes);
        ivNoData = dialogView.findViewById(R.id.ivNoData);
        tvCreateNote = dialogView.findViewById(R.id.tvCreateNote);

        tvCreateNote.setOnClickListener(v -> {
            String note = etNote.getText().toString();
            if (null != mCallback) {
                if (!TextUtils.isEmpty(note)) {
                    mCallback.onCreateNote(etNote.getText().toString());
                } else {
                    Tooast.normalInfo(mContext.getString(R.string.contentNotNull));
                }
            }
        });

        mNoteAdp = new NoteAdp();
        rvNotes.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        mNoteAdp.bindToRecycleView(rvNotes);

        ivClose.setOnClickListener(v -> {
            cancel();
        });
    }

    public void initData(List<NoteResponse.Data> noteList) {
        notes = noteList;
        etNote.setText("");
        mNoteAdp.clear();
        if (null != notes && notes.size() > 0) {
            rvNotes.setVisibility(View.VISIBLE);
            ivNoData.setVisibility(View.GONE);
            mNoteAdp.addAll(notes);
        } else {
            rvNotes.setVisibility(View.GONE);
            ivNoData.setVisibility(View.VISIBLE);
        }
    }

    public interface Callback {
        void onCreateNote(String note);
    }
}
